package com.heima.basic;

/**
 * @Author: Ethan Yankang
 * @Program: 面试必会代码
 * @Date: 2024-09-07 09:13
 **/
import java.util.Map;

public class PrintAllThreads {
    public static void main(String[] args) {
        // 获取当前所有活动线程的堆栈跟踪
        Map<Thread, StackTraceElement[]> allThreads = Thread.getAllStackTraces();
        int count=1;
        // 遍历所有线程，打印线程名称和状态
        for (Map.Entry<Thread, StackTraceElement[]> entry : allThreads.entrySet()) {
            Thread thread = entry.getKey();
            System.out.println("线程名称: " + thread.getName());
            System.out.println("线程ID: " + thread.getId());
            System.out.println("线程状态: " + thread.getState());
            System.out.println("是否为守护线程: " + thread.isDaemon());
            System.out.println("线程优先级: " + thread.getPriority());
            System.out.println("第"+count+++"个线程");
            System.out.println("--------------------------------");
        }
    }
}
